package com.dunqi.gpm.shunqing.common.util;

import android.app.Activity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;


/**
 * Created by Administrator on 2016/12/29 0029.
 */

public class WebUtil {

    public static void webSetting(WebView mWebView, Activity activity) {
        webSetting(mWebView, activity, true);
    }

    public static void webSetting(WebView mWebView, Activity activity, boolean viewPort) {
        mWebView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        WebSettings webSettings = mWebView.getSettings();

        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //如果访问的页面中有Javascript，则webview必须设置支持Javascript
//        mWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        设置H5的缓存是否打开，默认关闭。
        webSettings.setAppCacheEnabled(true);
//开启 database storage API 功能
        webSettings.setDatabaseEnabled(true);
//当你设置为true时，就代表你想要你的WebView支持多窗口，但是一旦设置为true，必须要重写WebChromeClient的onCreateWindow方法。
        webSettings.setSupportMultipleWindows(true);

        webSettings.setJavaScriptEnabled(true);
// 开启 DOM storage API 功能
        webSettings.setDomStorageEnabled(true);
//        缩放按钮的显示隐藏
//        webSettings.setBuiltInZoomControls(true);
        //播放视频
        webSettings.setPluginState(WebSettings.PluginState.ON);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //以下两条设置可以使页面适应手机屏幕的分辨率，完整的显示在屏幕上
        //设置是否使用WebView推荐使用的窗口
        webSettings.setUseWideViewPort(viewPort);
        //允许弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //防止中文乱码
        webSettings.setDefaultTextEncodingName("UTF-8");
        //设置WebView加载页面的模式
        webSettings.setLoadWithOverviewMode(true);

//        mWebView.addJavascriptInterface(new AllWebInterface(activity), "operation");//operation
//        mWebView.setWebChromeClient(new WebChromeClient.FileChooserParams());
    }

    public static void clearWebViewCache(WebView webView) {
        if (webView != null) {
            webView.clearHistory();
            webView.clearCache(true);
            webView.loadUrl("about:blank");
        }
    }

    //实时获取，因为时间不一样
//    @NonNull
//    public static Map<String, String> getMapHeader() {
//        Map<String, String> extraHeaders = new HashMap<>();
//        String source = MyOkhttpUtil.SOURCE;
//        String time = System.currentTimeMillis() + "";
//        UserJsonInfo.User user = UserUtil.getUser();
//
//        String code = "";
//        if (user != null) {
//            code = user.sign_code;
//        }
////        String data = SharedPreferencesUtils.getShareData(Contance.USER_CODE, false);
//
//        String s = Sha1Util.encryptToSHA(code + source + time);//sha1(code+source+timestamp)
//        String device_token = SharedPreferencesUtils.getShareData(MyInterface.DEVICE_TOKEN, false);
//        extraHeaders.put(MyInterface.DEVICE_TOKEN, device_token);
//        extraHeaders.put(MyInterface.SOURCE, source);
//        extraHeaders.put(MyInterface.TIMESTAMP, time);
//        extraHeaders.put(MyInterface.SIGN, s);
//        extraHeaders.put(MyInterface.CODE, code + "");
////        Log.d("onSuccess", "code=" + code);
//        return extraHeaders;
//    }

//    public static boolean syncCookie(String url) {
//
////        CookiesManager cookiesManager = CookiesManager.getInstence();
////
////        List<Cookie> cookies = cookiesManager.getCookies();
////
////        CookieManager cookieManager = CookieManager.getInstance();
////
////        cookieManager.setAcceptCookie(true);
////
////        for (Cookie cookie : cookies) {
////            String cookieName = cookie.name();
////            String cookieValue = cookie.value();
////            if (!TextUtils.isEmpty(cookieName)
////                    && !TextUtils.isEmpty(cookieValue)) {
////                cookieManager.setCookie(url, cookieName + "=" + cookieValue);
////            }
////        }
////        String cookie = cookieManager.getCookie(url);
//
////        if (!TextUtils.isEmpty(cookie)) {
////            Log.d("onSuccess", cookie);
////        }
//
//        return true;
//    }

//    public static void clearCookies() {
//        CookieSyncManager.createInstance(CommonUtil.getAppContext());
//        CookieSyncManager.getInstance().startSync();
//        CookieManager.getInstance().removeSessionCookie();
//        CookieManager.getInstance().removeAllCookie();
//    }

//    public static void addJSInterface(WebView webView, Activity a) {
//        webView.addJavascriptInterface(new AllWebInterface(a), "operation");
//    }
}
